<?php
	class UserIsLoggedInView
	{
		// Member Variables
		private $_navView = null;

		public function __construct()
		{
			$this->_navView = new NavigationView();
		}
		
		// Returns the menu depending on the usertype and number of messages the user have unread.
		public function DoMenu($userType, $numberOfNewMessages)
		{
			$profileView = new ProfileView();
			
			if ($userType == UserView::ADMIN)
			{
				if ($this->TriedToAdmin() == true || $this->TriedToDelete() == true)
				{
					return "<a href='" . $this->_navView->GetFormActionLink() . "' method='post'>" . NavigationView::LINKTEXTHOME . "</a>" . " " .
					"<a href='" . $this->_navView->GetAddUserAdress() . "' method='post'>" . NavigationView::LINKTEXTADDUSER . "</a>" . " " .
					"<a href='" . $this->_navView->GetViewErrorLogAdress() . "' method='post'>" . NavigationView::LINKTEXTVIEWERRORS . "</a>" . " ";
				}
				else if ($this->wantToAddUser() == true || isset($_POST[RegisterView::REGISTERED]) == true)
				{
					return "<a href='" . $this->_navView->GetFormActionLink() . "' method='post'>" . NavigationView::LINKTEXTHOME . "</a>" . " " .
					"<a href='" . $this->_navView->GetAdminAdress() . "' method='post'>" . NavigationView::LINKTEXTBACKTOADMIN . "</a><br />";
				}
				else if ($this->ViewingErrors() == true)
				{
					return "<a href='" . $this->_navView->GetFormActionLink() . "' method='post'>" . NavigationView::LINKTEXTHOME . "</a>" . " " .
					"<a href='" . $this->_navView->GetAdminAdress() . "' method='post'>" . NavigationView::LINKTEXTBACKTOADMIN . "</a><br />";
				}
				else 
				{
					$ret = "<a href='" . $this->_navView->GetFormActionLink() . "' method='post'>" . NavigationView::LINKTEXTHOME . "</a>" . " " .
					"<a href='" . $profileView->GenerateProfileAdress() . "' method='post'>" . NavigationView::LINKTEXTPROFILE . "</a>" . " " .
					"<a href ='" . $this->_navView->GetOptionAdress() ."' method='post'>" . NavigationView::LINKTEXTOTPIONS . "</a>" . " " .
					"<a href='" . $this->_navView->GetAdminAdress() . "' method='post'>" . NavigationView::LINKTEXTADMIN . "</a>" . " " .
					"<a href ='" . $this->_navView->GetMemberlistAdress() . "' method='post'>" . NavigationView::LINKTEXTMEMBERLIST . "</a>" . " ";
					if ($numberOfNewMessages != 0)
					{
						if ($numberOfNewMessages == 1)
						{
							$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . $numberOfNewMessages . NavigationView::LINKTEXTNEWMESSAGE . "</a><br />";
						}
						else 
						{
							$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . $numberOfNewMessages . NavigationView::LINKTEXTNEWMESSAGES . "</a><br />";
						}
					}
					else 
					{
						$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . NavigationView::LINKTEXTMESSAGING . "</a><br />";
					}
					return $ret;
				}
			}
			else 
			{
				$ret = "<a href='" . $this->_navView->GetFormActionLink() . "' method='post'>" . NavigationView::LINKTEXTHOME . "</a>" . " " .
					"<a href='" . $profileView->GenerateProfileAdress() . "' method='post'>" . NavigationView::LINKTEXTPROFILE . "</a>" . " " .
					"<a href ='" . $this->_navView->GetOptionAdress() . "' method='post'>" . NavigationView::LINKTEXTOTPIONS . "</a>" . " " .
					"<a href ='" . $this->_navView->GetMemberlistAdress() . "' method='post'>" . NavigationView::LINKTEXTMEMBERLIST . "</a>" . " ";
				if ($numberOfNewMessages != 0)
				{
					if ($numberOfNewMessages == 1)
					{
						$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . $numberOfNewMessages . NavigationView::LINKTEXTNEWMESSAGE . "</a><br />";
					}
					else 
					{
						$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . $numberOfNewMessages . NavigationView::LINKTEXTNEWMESSAGES . "</a><br />";
					}
				}
				else 
				{
					$ret .= "<a href ='" . $this->_navView->GetMessagingAdress() . "' method='post'>" . NavigationView::LINKTEXTMESSAGING . "</a><br />";
				}
				return $ret;
			}
		}
		
		// Returns wether an admin tried to use the adminpage.
		public function TriedToAdmin()
		{
			if (isset($_GET[NavigationView::IWANTTOADMIN]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether an admin tried to delete a user.
		public function TriedToDelete()
		{
			if (isset($_GET[LoginView::DELETE]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether an admin is viewing the error-log.
		public function ViewingErrors()
		{
			if (isset($_GET[NavigationView::IWANTTOVIEWERRORS]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether a user is viewing the memberlist.
		public function ViewingMemberlist()
		{
			if (isset($_GET[NavigationView::IWANTTOVIEWMEMBERLIST]) == true)
			{
				return true;
			}
			return false;
		}
		
		// Returns wether an admin wants to add a user.
		public function WantToAddUser()
		{
			if (isset($_GET[NavigationView::IWANTTOADDUSER]) == true)
			{
				return true;
			}
			return false;
		}		
		
		// Returns the ID of the user to be deleted.
		public function GetDeleteId()
		{
			return $_GET[NavigationView::IWANTTODELETEID];
		}
		
		// Returns the username of the user to be deleted.
		public function GetDeleteUsername()
		{
			return $_GET[NavigationView::IWANTTODELETEUSERNAME];
		}

		// Returns html of a list of all users in the database.
		public function ListUsers(Array $userList)
		{
			$xhtml = "";
			$xhtml .= "<br /><b>id | username | usertype | givenname surname | email</b><br />";
			
			foreach($userList as $value)
			{
				$xhtml.= $value->id . " | " .$value->username . " | " . $value->usertype . " | " . 
				$value->givenname . " " . $value->surname . " | " . $value->email . $this->DeleteButton($value);
			}
			
			return $xhtml;
		}
		
		// Returns html for the delete button.
		public function DeleteButton($value)
		{
			return "<form method='get' action='" . $this->_navView->GetAdminAdress() . "'>" .
					"<input id='delId' name='" . NavigationView::IWANTTODELETEID . "' type='hidden' value='$value->id'>" .
					"<input id='delUsername' name='" . NavigationView::IWANTTODELETEUSERNAME . "' type='hidden' value='$value->username'>" .
					"<input id='delete' name='" . LoginView::DELETE . "' type='submit' value='Delete'>" .
			"</form>";
		}
		
	}